home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Essentials / C++ AppleLink Messages / CPlus.Dev$ 4⁄6⁄90 / 0097-Who's at home base?-Apr90 < prev    next >
Encoding:
Text File  |  1990-04-06  |  2.1 KB  |  71 lines  |  [TEXT/GEOL]

  1. Item    9251348                         5-April-90        13:14PDT
  2.  
  3. From:   D0532                           Aidea Systems, Don Park,PRT
  4.  
  5. To:     CPLUS.DEV$                      C++ Interest List--Developers
  6.         CPLUS.APPLE$                    C++ Interest List--Apple Employees
  7.  
  8. Sub:    Who's at home base?
  9.  
  10. This is a follow up on my memo titled "Who's on first?".
  11.  
  12. After reading the books like a hound dog and experimenting further, I have
  13. concluded the following:
  14.  
  15. 1.  One can not control order of static object instantiation in C++.
  16. 2.  MPW Link instantiates static objects in the order of linking.
  17.  
  18. The memos others sent me on the subject agrees.
  19.  
  20. This implies that, in general, cout/cin/cerr can not be used in the
  21. constructors of static objects since cout/cin/cerr themselves are static
  22. objects in the stream library.  For example:
  23.  
  24. #include <stream.h>
  25.  
  26. class Test
  27. {
  28. public:
  29.    Test (void) { cout << "test constructor!\n"; }
  30.    ~Test (void) { cout << "test destructor!\n"; }
  31.  void Null (void) { cout << "test null!\n"; }
  32. };
  33.  
  34. Test   test;
  35.  
  36. main ()
  37. {
  38.    test.Null();
  39. }
  40.  
  41. Above code generates following:
  42.  
  43. test null!
  44.  
  45. This is pretty sad for debugging.  Under MPW however, one can get around this
  46. by linking CPlusLib.o before the above code.  Automatically generated make
  47. files link CPlusLib.o AFTER user object files so one has to edit the make file
  48. manually to do this.
  49.  
  50. After the fix, the output is:
  51.  
  52. test constructor!
  53. test null!
  54. test destructor!
  55.  
  56. Great for MPW, still sad for others.
  57.  
  58. There is a way to get around all this with a little organizing.  One can
  59. declare all static objects as members of a single static object, controlling
  60. the order of instantiation by the order of the members.  While I don't know if
  61. this was the motive, same approach was used in Keith Gorlen's NIHCL (National
  62. Institutes of Health Class Library).  NIHCL has at its root a class named NIHCL
  63. and all other classes, including Object class, are derived from it.
  64.  
  65. So we go from "Who's on first?" to "Who's at home base?"
  66.  
  67. Don Park
  68.  
  69. P.S.    Thanks to Joe MacDougald, Debra Orton, and Ed Ruder for replying.
  70.  
  71.